function [ segment] = getSegmentLists( imSkel )
%UNTITLED2 Summary of this function goes here
%   Returns a structure array of PixelLists for each segment.  The pixel
%   lists do not contain the branchpoints.

    %Get a list of of branchpoints and their neighbors for each branch
    
    
    %% Create a structure array containing branchpoints and their neighbors
    
    branchpoints = bwmorph(imSkel, 'branchpoints');
    [row ,col]  = find(branchpoints);
    branchpoint = [row,col];
    numBranchpoints = size(branchpoint,1);
    branchpt = struct('Neighbors',[],'Branchpoint',[]);
    
    for k = 1:numBranchpoints
        %Get a 3 x3 matrix of the enighborhood not including the pixel
        neighbors = getNeighbors(imSkel, branchpoint(k,:));
        branchpt(k).Neighbors =  neighbors;
        branchpt(k).Branchpoint = branchpoint(k,:);
    end
    
    %% Create an image of disjoint skeleton pieces
    
    % Remove branchpoints and all neighbors of branchpoints to obtain the
    % disjoint skeleton pieces.  Removing neighbors of branchpoints is
    % necessary since for a T the segments are neighbors, adn therefore
    % connected, even if the branchpoint is removed.
    
    imSkelTemp = imSkel;
    
        
        %Subtract the pixels representing the branchpoints and their
        % neighbors from the original image to break it up
        for k = 1:numBranchpoints
            bp = branchpt(k).Branchpoint;
            nb = branchpt(k).Neighbors;
        indexBranchpoints = sub2ind(size(imSkel),bp(:,1), bp(:,2));
        neighIndices = sub2ind(size(imSkel),nb(:,1), nb(:,2) );
        imSkelTemp(neighIndices) = 0;
        imSkelTemp(indexBranchpoints)= 0; 
       
        end
    
    
    props = regionprops(imSkelTemp,'PixelIdxList');
      
    %A structure to storge the pixellists for each segment
    segment = struct('PixelList',[]);
    
    %For each segment
    for i = 1:length(props)
        
        %Create an image with only the current segment and then get the
        %pixel list of the segment
        indices = props(i).PixelIdxList;
        tempImage = zeros(size(imSkel));
        tempImage(indices) = 1;
        pixelList = getSegmentPixels(tempImage);
        addToBeginning = [];
        addToEnd = [];
        %Iterate though all neighbors of all branchpoints to see if any
        %should be attached to the pixelList
        for j = 1:length(branchpt)
             neighbors = branchpt(j).Neighbors;
             theBranchpoint = branchpt(j).Branchpoint;
              for k = 1 : size(neighbors,1)
                    %Does the brnachpoint neighbor connect to the beginning
                    %of the list?
                    if isNeighbor( neighbors(k,:), pixelList(1,:))
                        addToBeginning = [theBranchpoint;neighbors(k,:)];
                    %Does the branchpoint neighb connect to the end of the
                    %list
                    elseif isNeighbor( neighbors(k,:), pixelList(size(pixelList,1),:))
                        addToEnd = [neighbors(k,:);theBranchpoint] ;
                    else
                        %Do nothing
                    end
              end
        end
        %Save the final pixel list
        
        segment(i).PixelList = [addToBeginning;pixelList;addToEnd]; 
    end
    
       

    %% for 2 or 3 pixels separating two branchpoints.
    
    %These short branches ae lost when larger segments are split apart by 
    % deleting the brnachpoint neighborhood from the image.
    
      
    %Create an image with short segments not identified above by subtracting all
    %the known segments and their associated branchpoints from the original
    %skeleton image
    imTemp = imSkel;
    for i = 1:length(segment)
        idx = sub2ind(size(imSkel), segment(i).PixelList(:,1), segment(i).PixelList(:,2));
        imTemp(idx) = 0;
    end
    
    %Iterate through the branchpoints to see if they attach to the
    %identified segments.
    props = regionprops(imTemp, 'PixelIdxList');
    if ~isempty(props)
        for i = 1:length(props)

            %Create an image with only the current segment and then get the
            %pixel list of the segment
            tempImage = zeros(size(imSkel));
            idx = props(i).PixelIdxList;
            tempImage(idx) = 1;

            pixelList = getSegmentPixels(tempImage);
            %For each segment

            addToBeginning = [];
            addToEnd = [];
            %Iterate though all branchpoints to see if any
            %should be attached to the current segment
            if size(pixelList,1) == 1
                newPixelList = pixelList(1,:);
                oneAppended = 0;
                for j = 1:length(branchpt)
                      theBranchpoint = branchpt(j).Branchpoint;
                      if isNeighbor( theBranchpoint, pixelList(1,:))
                           if oneAppended
                             newPixelList = [newPixelList;theBranchpoint];
                           else
                               oneAppended = 1;
                               newPixelList = [theBranchpoint;newPixelList;];
                           end
                      end
                end
            else
                %If more than one pixel in the pixel list
                for j = 1:length(branchpt)
                      theBranchpoint = branchpt(j).Branchpoint;
                      if isNeighbor( theBranchpoint, pixelList(1,:))
                           addToBeginning = [theBranchpoint];
                      elseif isNeighbor( theBranchpoint, pixelList(size(pixelList,1),:))
                                addToEnd = [theBranchpoint] ;
                      else
                                %Do nothing
                      end
                end
                newPixelList = [addToBeginning;pixelList;addToEnd]; 
            end

            %Append results to the known segments
            len = length(segment);
            segment(len+1).PixelList = newPixelList;
        end


      %% Search for two branchpoints next to eachother

        if length(branchpt) > 1
            for i = 1:length(branchpt)
                for j = 1+1:1:length(branchpt);

                    if isNeighbor(branchpt(i).Branchpoint,...
                                  branchpt(j).Branchpoint)...
                       && ~isequal(branchpt(i).Branchpoint,...
                                   branchpt(j).Branchpoint)
                        len = length(segment);
                        segment(len+1).PixelList =...
                                [branchpt(i).Branchpoint;branchpt(j).Branchpoint];
                    end
                end
            end
        end
    
    end
     
    
    
end
        
    
  
    